package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.mapper.EmployeeMapper;
import cn.wolfcode.rbac.pojo.Employee;
import cn.wolfcode.rbac.service.EmployeeService;
import cn.wolfcode.rbac.util.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> selectall() {
        log.info("{}", employeeMapper.selectall());
        return employeeMapper.selectall();
    }

    @Override
    public PageInfo<Employee> selectallpage(QueryObject queryObject) {
        PageHelper.startPage(queryObject.getCurrentPage(), queryObject.getPageSize());
        List<Employee> employeeList = employeeMapper.selectallq(queryObject);
        return new PageInfo<>(employeeList);
    }

    @Override
    public Employee selectbyid(Integer id) {
        Employee employee = employeeMapper.selectbyid(id);
        return employee;
    }

    @Override
    public void save(Employee employee) {
        if (employee.getId() != null) {
            employeeMapper.updateemployee(employee);
            employeeMapper.updatedepartment(employee);
        }
        employeeMapper.save(employee);
    }
}
